package com.joey.sf.no1;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class MathUtils {
    public static void main(String[] args) {
        new MathUtils().testShuffle();
    }

    /**
     * 质数 优化版本
     */
    public static boolean isPrime(int num) {
        if (num < 2) {
            return false;
        }
        for (int i = 2; i * i <= num; i++) {
            if (num % i == 0) {
                return false;
            }
        }
        return true;
    }

    /**
     * 开平方根 牛顿迭代法
     */
    public static double sqrt(double c) {
        if (c < 0) return Double.NaN;
        double error = 1e-15;
        double t = c;
        while (Math.abs(t - c / t) > error * t) {
            t = (c / t + t) / 2.0;
        }
        return t;
    }

    /**
     * 离散概率返回随机数
     */
    public static int random(double[] a) {
        double random = Math.random();
        double sum = 0.0d;
        for (int i = 0; i < a.length; i++) {
            sum += a[i];
            if (sum >= random) {
                return i;
            }
        }
        return -1;
    }

    /**
     * 洗牌
     */
    public static void shuffle(Object[] array) {
        int replacePosition = 0;
        for (int position = 0, size = array.length; position < size; position++) {
            replacePosition = replacePosition + (int) (Math.random() * (size - position - 1));
            Object temp = array[position];
            array[position] = array[replacePosition];
            array[replacePosition] = temp;
        }
    }

    /**
     * 洗牌
     */
    public static void shuffle(int[] array) {
        int replacePosition = 0;
        for (int position = 0, size = array.length; position < size; position++) {
            replacePosition = position + (int) (Math.random() * (size - position - 1));
            int temp = array[position];
            array[position] = array[replacePosition];
            array[replacePosition] = temp;
        }
    }

    public void testShuffle() {
        String[] array = {"我", "是", "锅"};
        int max = 1_0000_00;
        System.out.println("测试shuffle方法 数组为：" + Arrays.toString(array) + " 总次数：" + max);
        Map<Integer, Map<Object, Integer>> map = new HashMap<>();
        for (int i = 0; i < max; i++) {
            shuffle(array);
            for (int position = 0; position < array.length; position++) {
                String text = array[position];
                Map<Object, Integer> textMap = map.get(position);
                if (textMap == null) {
                    textMap = new HashMap<>();
                    map.put(position, textMap);
                }

                Integer integer = textMap.get(text);
                if (integer == null) {
                    integer = 0;
                }
                textMap.put(text, integer + 1);
            }
        }

        for (Map.Entry<Integer, Map<Object, Integer>> entry : map.entrySet()) {
            Integer key = entry.getKey();
            for (Map.Entry<Object, Integer> textEntry : entry.getValue().entrySet()) {
                Object text = textEntry.getKey();
                Integer count = textEntry.getValue();
                String percent = count * 100 / max + "%";
                System.out.println("position=" + key + " object=" + text + " 的概率是=" + percent);
            }
        }
    }
}
